﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class home : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {

        lbldatetime.Text = DateTime.Now.ToString();
        bussiness bus = new bussiness();
        List<SentEcard> lstSentEcard = bus.getAllSentEcard();
        List<listEmail> lstListEmail = bus.getAllListEmail();
        foreach (SentEcard se in lstSentEcard)
        {
            int i = 0;
            i++;
            if (se.DateSent.Day == DateTime.Now.Day && se.DateSent.Month == DateTime.Now.Month && se.DateSent.Year == DateTime.Now.Year)
            {
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = new NetworkCredential("vanthanh26041989@gmail.com", "vanthanh");
                MailMessage message = new MailMessage();
                //Message object must be disposed before deleting temp attachment files

                message.From = new MailAddress("vanthanh26041989@gmail.com", "tranthanh");
                message.Subject = "thanh sent for you 1 ecard happy birhday";
                message.Body = "hi tennguoinhan! ";
                message.IsBodyHtml = true;
                foreach (listEmail email in lstListEmail)
                {
                    if (email.SentEcardId == se.SentEcardId)
                    {
                        message.To.Add(email.Email);
                    }
                }
                try
                {
                    smtpClient.Send(message);
                }
                catch (Exception ex) {
                    Response.Write(ex.ToString());
                }
            }

        }

    }
}
